/* -------------------------------------------------------------------------- *
   TestWinUnit - Maria Blees (maria.blees@microsoft.com)

   File: ReadableTempFile.cpp
   This file/class is a specialization of TempFile that allows reading the 
   contents of the file.
 * -------------------------------------------------------------------------- */
#include "ReadableTempFile.h"

#include <share.h>
#include "WinUnit.h"

ReadableTempFile::ReadableTempFile()
{
    _file = _wfsopen(this->Name, L"rtc", _SH_DENYNO);
}

ReadableTempFile::~ReadableTempFile()
{
    if (_file)
    {
        fclose(_file);
    }
}

char* ReadableTempFile::ReadLine(
      char* buffer, DWORD bufferSize, size_t& charsRead, bool& truncated)
{
    if (feof(_file)) { return NULL; }
    if (fgets(buffer, bufferSize, _file) == NULL)
    {
        return NULL;
    }
    charsRead = strlen(buffer);
    truncated = true;

    // If there's a newline, replace with '\0' and unset "truncated" flag
    if (charsRead > 0 && buffer[charsRead-1] == '\n')
    {
        buffer[--charsRead] = '\0';
        truncated = false;
    }

    return buffer;
}
